package de.slothsoft.wasbinich.swingclient.control;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.slothsoft.wasbinich.swingclient.res.Images;

public class TitlePanel extends JPanel implements SwitchPanel {

    private static final long serialVersionUID = 650810276744021503L;

    private static final int BORDER_BUTTONS = 5;

    private final ScreenSwitcher screenSwitcher;
    private JPanel mainPanel;

    public TitlePanel(ScreenSwitcher screenSwitcher) {
	this.screenSwitcher = screenSwitcher;
	setOpaque(false);
	setLayout(new BorderLayout());
	add(createTitleImage(), BorderLayout.CENTER);
	add(createControls(), BorderLayout.EAST);
    }

    private Component createTitleImage() {
	return new JLabel(Images.get(Images.OBJECT_TOAD));
    }

    private JComponent createControls() {
	JPanel wrapper = new JPanel();
	wrapper.setLayout(new BorderLayout());
	wrapper.setOpaque(false);

	GridLayout layout = new GridLayout(0, 1);
	layout.setHgap(BORDER_BUTTONS);
	layout.setVgap(BORDER_BUTTONS);

	this.mainPanel = new JPanel();
	this.mainPanel.setOpaque(false);
	this.mainPanel.setLayout(layout);
	this.mainPanel.setBorder(BorderFactory.createEmptyBorder(BORDER_BUTTONS, BORDER_BUTTONS, BORDER_BUTTONS,
		BORDER_BUTTONS));
	wrapper.add(this.mainPanel, BorderLayout.NORTH);
	wrapper.add(new JLabel(), BorderLayout.CENTER);

	return wrapper;
    }

    public JButton addScreen(final String id, String title) {
	JButton button = new WasBinIchButton(title);
	button.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		getScreenSwitcher().switchScreen(id);
	    }
	});
	this.mainPanel.add(button);
	return button;
    }

    public JButton addButton(String title, ActionListener actionListener) {
	JButton button = new WasBinIchButton(title);
	button.addActionListener(actionListener);
	this.mainPanel.add(button);
	return button;
    }

    protected ScreenSwitcher getScreenSwitcher() {
	return this.screenSwitcher;
    }

    @Override
    public void showPanel() {
	// nothing to do
    }

    @Override
    public void hidePanel() {
	// nothing to do
    }

    /*
	 * 
	 */

    static interface ScreenSwitcher {

	SwitchPanel switchScreen(String id);
    }
}
